package com.yisu.expressway.onedollar.presenter.impl;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.pingplusplus.android.Pingpp;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.onedollar.activity.PayActivity;
import com.yisu.expressway.onedollar.model.ChargeObj;
import com.yisu.expressway.onedollar.presenter.IPayPresenter;
import com.yisu.expressway.onedollar.view.IPayView;
import com.yisu.expressway.utils.ToastUtil;

import android.app.Activity;
import android.content.Context;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Kaven on 2016/12/19.
 */

public class PayPresenterImpl implements IPayPresenter {
    private Context mContext;
    private IPayView mView;
    public static final int CHANNEL_APLIPAY = 1;
    public static final int CHANNEL_WEIXIN = 2;
    public PayPresenterImpl(Context context, IPayView iPayView) {
        this.mContext = context;
        this.mView = iPayView;
    }

    @Override
    public void init() {

    }

    @Override
    public void cancelOrder(long orderId) {
        Map<String, Object> params = new HashMap();

        params.put("orderId", orderId);
        ApiClientHelper.postApi(ApiSetting.cancleOrder(), new TypeToken<Object>() {
        },  new JSONObject(params),new Response.Listener<ApiResult<Object>>() {
            @Override
            public void onResponse(final ApiResult<Object> result) {

                if (!result.success) {
                    mView.showToast(result.getMsg());
                    return;
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mView.showToast(volleyError.getMessage());
            }
        }, mContext);
    }

    @Override
    public void changeOrderPaymentChannel(long orderId, final int paymentMethod) {
        if (paymentMethod < 0) {
            return;
        }
        if (orderId <= 0) {
            return;
        }
        Map<String, Object> params = new HashMap();

        params.put("orderId", orderId);
        params.put("paymentMethod", paymentMethod);
        ApiClientHelper.postApi(ApiSetting.changeOrderPayment(), new TypeToken<Object>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<Object>>() {
            @Override
            public void onResponse(ApiResult<Object> result) {
                if (!result.success) {
                    mView.showToast(result.getMsg());
                    return;
                }
                mCurrentChannel = paymentMethod;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mView.showToast(volleyError.getMessage());
            }
        }, this);
    }
    private int mCurrentChannel =CHANNEL_APLIPAY;
    public int getPayChannel() {
        return mCurrentChannel;
    }


    public void getPaymentData(long orderId, int paymentChannel) {
        if (orderId <= 0) {
            return;
        }
        Map<String, Object> params = new HashMap();

        params.put("orderId", orderId);
        params.put("paymentChannel", paymentChannel);
        ApiClientHelper.postApi(ApiSetting.getPaymentData(), new TypeToken<ChargeObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ChargeObj>>() {
            @Override
            public void onResponse(ApiResult<ChargeObj> result) {
                if (!result.success) {
                   mView.showToast(result.getMsg());
                    return;
                }
                String charge = result.getResult().chargeObj;
                startPayTask(mView.getActivity(),charge);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mView.showToast(volleyError.getMessage());
            }
        }, this);
    }

    public void startPayTask(Activity activity,String charge) {
        Pingpp.createPayment(activity, charge);
    }
}
